package com.nelson.musicplayer.util

import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.Toast
import androidx.annotation.StringRes
import com.bumptech.glide.Glide
import com.nelson.musicplayer.appContext
import com.nelson.musicplayer.constant.TAG
import com.nelson.musicplayer.constant.isDebug
import android.content.res.Resources

/**
 * @author nelson
 * @since 2020-07-25
 */

fun toast(msg: String) {
    Toast.makeText(appContext, msg, Toast.LENGTH_SHORT).show()
}

fun toast(@StringRes res: Int) {
    Toast.makeText(appContext, appContext.getString(res), Toast.LENGTH_SHORT).show()
}

fun logD(msg: String) {
    if (isDebug) {
        Log.i(TAG, msg);
    }
}

fun logD(tag: String, msg: String) {
    if (isDebug) {
        Log.i(tag, msg);
    }
}

fun loadImage(imageView: View?, path: String) {
    (imageView as? ImageView)?.let {
        Glide.with(appContext).load(path).into(it)
    }
}

fun loadServerImage(imageView: View?, path: String) {
    (imageView as? ImageView)?.let {
        Glide.with(appContext).load("http:$path").into(it)
    }
}

/**
 * 正常编码中一般只会用到 [dp]/[sp] ;
 * 其中[dp]/[sp] 会根据系统分辨率将输入的dp/sp值转换为对应的px
 */
val Float.dp: Float                 // [xxhdpi](360 -> 1080)
    get() = android.util.TypedValue.applyDimension(
        android.util.TypedValue.COMPLEX_UNIT_DIP, this, Resources.getSystem().displayMetrics
    )

val Int.dp: Int
    get() = android.util.TypedValue.applyDimension(
        android.util.TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), Resources.getSystem().displayMetrics
    ).toInt()


val Float.sp: Float                 // [xxhdpi](360 -> 1080)
    get() = android.util.TypedValue.applyDimension(
        android.util.TypedValue.COMPLEX_UNIT_SP, this, Resources.getSystem().displayMetrics
    )


val Int.sp: Int
    get() = android.util.TypedValue.applyDimension(
        android.util.TypedValue.COMPLEX_UNIT_SP, this.toFloat(), Resources.getSystem().displayMetrics
    ).toInt()